package com.zy.qudadid.presenter;

import android.util.Log;

import com.zy.bean.Dingdan;
import com.zy.qudadid.model.DownWindStroke;
import com.zy.qudadid.model.Res;
import com.zy.qudadid.model.User;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.view.MainView;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ASUS on 2017/2/28.
 */

public class MainPresenter extends BasePresenterImp<MainView>{

    //乘客叫车
    public void CallCar(String data){
        Subscription subscription = Net.getService()
                .CallCar(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<Dingdan>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("CallCar_onError","叫车出错:" + e.getMessage().toString());
                        view.callCarError("附近没有司机");
//                        if(e.getMessage().toString().equals("java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 173 path $.datas")) {
//                            view.callCarError("已重复叫车");
//                        }
                        //ToolsUtil.print("----", "司机端推送错误:" + e.getMessage().toString());
                    }

                    @Override
                    public void onNext(Res<Dingdan> res) {
                        if (res.code == Const.OK) {
                            view.callCarSuccess(res.datas);
                        } else {
                            Log.e("CallCar_onNext","叫车出错:" + res.message);
                            view.callCarError(res.message);
                        }
                    }
                });

        addSubscription(subscription);
    }

    //乘客叫顺风车
    public void CallCar2(String data){
        Subscription subscription = Net.getService()
                .CallCar2(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<Dingdan>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("CallCar2_onError","叫车出错:" + e.getMessage().toString());
                        view.callCarError(e.getMessage().toString());
                        e.printStackTrace();


//                        if(e.getMessage().toString().equals("java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 173 path $.datas")) {
//                            view.callCarError("已重复叫车");
//                        }
                        //ToolsUtil.print("----", "司机端推送错误:" + e.getMessage().toString());
                    }

                    @Override
                    public void onNext(Res<Dingdan> res) {
                        if (res.code == Const.OK) {
                            view.callCarSuccess(res.datas);
                        } else {
                            Log.e("CallCar2_onNext","叫车出错:" + res.message);
                            view.callCarError(res.message);
                        }
                    }
                });

        addSubscription(subscription);
    }

    //乘客取消订单（不拼车，拼车，单方面取消）
    public void CancelCallCar(String data){
        Subscription subscription = Net.getService()
                .CancelCallCar(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<Dingdan>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.exception();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res<Dingdan> res) {
                        if (res.code == Const.OK) {
                            view.cancelCallCarSuccess();
                        } else {
                            view.cancelCallCarError(res.message);
                        }
                    }
                });

        addSubscription(subscription);
    }

    //司机乘客查询个人信息
    public void InquireInfo(String data){
        Subscription subscription = Net.getService()
                .InquireInfo(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<User>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Res<User> res) {
                        int num = 0;
                        if (res.code == Const.OK) {
                            view.successInquireInfo(res.datas);
                        }
                    }
                });

        addSubscription(subscription);
    }

    //乘客是否确认上车司机确认上车(不拼车，拼车共用)
    public void Boarding(String data){
        Subscription subscription = Net.getService()
                .Boarding(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<Dingdan>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("onError","确认上车失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res<Dingdan> res) {
                        int num = 0;
                        if (res.code == Const.OK) {
                            view.successBoarding(res.datas);
                        }else{
                            view.errorBoarding(res.message);
                        }
                    }
                });

        addSubscription(subscription);
    }

    //获取顺风车已发布行程、已完成行程各一条
    public void MyStroke(){
        Map mapTravel = new HashMap();
        mapTravel.put("user_id", new UserUtil(view.getContext()).getUserId());
        mapTravel.put("type", "1");
        mapTravel.put("car_type", 5);
        mapTravel.put("finished", 1);//1,完成；2未完成
        mapTravel.put("nowpage", 1);
        mapTravel.put("page_num", 1);
        String jsonTravel = StringUtil.toJson(mapTravel);
        String d="";
        try {
            d = AESCrypt.getInstance().encrypt(jsonTravel);
            d = StringUtil.replaceBlank(d);
        } catch (Exception ex) {
        }

        Subscription subscription1 = Net.getService()
                .MyStroke2(d)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<DownWindStroke>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("onError","获取顺风车已发布行程、已完成行程失败:"+e.getMessage());
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res<DownWindStroke> res) {
                        if (res.code == Const.OK) {
                            view.successMyStroke(res.datas);
                            Log.e("onNext","获取顺风车已发布行程、已完成行程成功");
//                            view.successMyStroke(res.datas.infor);
//                            setDataStatus(res.datas.infor);
                        } else {
                            Log.e("onNext","获取顺风车已发布行程、已完成行程失败");
                            view.errorMyStroke();
//                            view.error();
                        }
                    }
                });

        addSubscription(subscription1);

    }

    //出租车费用，里程，时间估算预估（高得地图）
//    public void EstimatedFare(String data){
//        Subscription subscription = Net.getService()
//                .EstimatedFare(data)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<Res<User>>() {
//
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(Res<User> res) {
//                        int num = 0;
//                        if (res.code == Const.OK) {
//                            view.successInquireInfo(res.datas);
//                        }
//                    }
//                });
//
//        addSubscription(subscription);
//    }
}
